package com.hehe.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "")
public class PayTestController {


    @Value("${config_file_name}")
    private String config_file_name;


//    @Resource
//    private UserRepository userRepository;


    @GetMapping(value = "")
    public String aaaaaaaaaaa() {
        System.out.println("访问 index 页面...");
        System.out.println("访问 index 页面...");
        return "test.html";
    }
    @GetMapping(value = "/test")
    public String bbbbb() {
        System.out.println("访问 TEST 页面...");
        System.out.println("访问 TEST 页面...");
        return "test.html";
    }

    //    ResponseBody
    @GetMapping(value = "/api/test/buy")
    @ResponseBody
    public String ResponseBody_test() {

            String orderIdStr = StringTool.getRandomString();

//            Orders byUsernameAndPhotoId = ordersRepository.findByUsernameAndPhotoId(username, (int) id);
//            if (byUsernameAndPhotoId != null) {
//                // 已经支付过了
//                orderIdStr = byUsernameAndPhotoId.getOrderId();
//            }
            // 如果已经支付 (订单号相同)
            // 虎皮椒支付 接口 响应的结果是：{"errcode":500,"errmsg":"当前订单号已成功支付！","hash":"07b8e364e905595dcf47189042fb7129"}
            // 还没有支付:
            // 虎皮椒支付 接口 响应的结果是：
            // {"openid":20263100604,
            // "url_qrcode":"https://api.xunhupay.com/payments/alipay/qrcode?id=20263100604&nonce_str=4182407043&time=1724440083&appid=201906167140&hash=8a04917d9b6a57c11ca6a483f6b2d256",
            // "url":"https://api.xunhupay.com/payments/alipay/index?id=20263100604&nonce_str=4872410304&time=1724440083&appid=201906167140&hash=0014270f0f69507eebb38253f6e50d55",
            // "errcode":0,"errmsg":"success!","hash":"f4e677995de119d3a12d104137b62aba"}
            String payResult = PayTool.pay(0.01, "摄影", orderIdStr, "successURL");
            return payResult;
//            if (payResult.contains("errmsg") && payResult.contains("当前订单号已成功支付")){
//                // 订单已经支付, 保存数据库
//                Orders order = new Orders();
//                order.setUsername(username);
//                order.setPhotoId((int) id);
//                order.setOrderId(orderIdStr);
//                order.setOrderPrice(String.valueOf(priceDouble));
//                ordersRepository.save(order);
//                return photo.getNetdiskLink();  // 获取网盘链接
//            }
//            if (payResult.contains("url_qrcode") && payResult.contains("success")){
//                // 订单未支付, 二维码支付
//                return payResult;
        }

// 完整的 URL，可以使用 request.getRequestURL() 方法
//        StringBuffer requestURL = request.getRequestURL();
//        String successURL = "";
//        if (requestURL.toString().contains("53jiepai.com")) {
//            successURL = "https://www.53jiepai.com";
//        } else {
//            successURL = "http://192.168.8.194:8055";
//        }
//        String username = "visitor";
//        successURL = String.format("%s/buy/success/%d/%s/%s", successURL, id, username, orderIdStr);
    }




















